package chat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Client {
	private BufferedReader in;
	private PrintWriter out;
	private JFrame frame = new JFrame("Chatter");
	private JTextField textField = new JTextField(40);
	private JTextArea messageArea = new JTextArea(8, 40);
	private final int PORT = 7331;
	private String configFile = "src/chat/config";


	public Client() {
		textField.setEditable(false);
		messageArea.setEditable(true);
		messageArea.setAutoscrolls(true);
		messageArea.setLineWrap(true);
		frame.getContentPane().add(textField, "North");
		frame.getContentPane().add(new JScrollPane(messageArea), "Center");
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		frame.pack();

		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				out.println(textField.getText());
				textField.setText("");
			}
		});

	}
	
	private String setServerAddress() throws IOException {
		File f = new File(configFile);
		FileReader fr = new FileReader(f);
		BufferedReader bfr = new BufferedReader(fr);
		String st = "";
		st = bfr.readLine();
		return st;
	}

//	private String getServerAddress() {		
//		return JOptionPane.showInputDialog(frame,
//				"Enter the IP address of the Server:",
//				"Welcome to the Chatter", JOptionPane.QUESTION_MESSAGE);
//	}

	private String getName() {
		return JOptionPane.showInputDialog(frame, "Choose a screen name: ",
				"Screen name Selection", JOptionPane.PLAIN_MESSAGE);
	}
	
	private void nameRedo() {
		JOptionPane.showMessageDialog(frame, "Username already in use", "Try Again", JOptionPane.ERROR_MESSAGE);
	}

	private void run() throws UnknownHostException, IOException  {
		String serverAddress = setServerAddress();
		//System.out.println(serverAddress); used for debugging
		Socket socket = new Socket(serverAddress, PORT);
		in = new BufferedReader(new InputStreamReader(
				socket.getInputStream()));
		out = new PrintWriter(socket.getOutputStream(), true);
		
		while (true) {
			String line = in.readLine();
			if (line.startsWith("SUBMITNAME")) {
				out.println(getName());
			} else if (line.startsWith("NAMEACCEPTED")) {
				textField.setEditable(true);
			} else if (line.startsWith("MESSAGE")) {
				messageArea.append(line.substring(7) + "\n");
				messageArea.setCaretPosition(messageArea.getDocument().getLength());
			} else if (line.startsWith("KILL")) {
				socket.close();
				System.exit(0);
			} else if (line.startsWith("NAMEAGAIN")){
				nameRedo();
				out.println(getName());
			} else {
				messageArea.append(line + "\n");
			}
		}
	}

	public static void main(String[] args) throws UnknownHostException,
			IOException {
		Client client = new Client();
		client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		client.frame.setVisible(true);
		client.run();
	}

}
